package com.yzh.wanandroid.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.RecyclerView
import com.youth.banner.adapter.BannerAdapter
import com.yzh.wanandroid.R
import com.yzh.wanandroid.databinding.ItemViewpagerLayoutBinding
import com.yzh.wanandroid.network.retrofit.responsebean.TopBannerData


class HomeBannerAdapter(mDatas: List<TopBannerData>) : BannerAdapter<TopBannerData,
        HomeBannerAdapter.BannerItemViewHolder>(mDatas) {

    private lateinit var mOnBannerClickListener: (View,String,String) -> Unit

    fun setOnBannerClickListener(onBannerClickListener: (View,String,String) -> Unit){
        this.mOnBannerClickListener = onBannerClickListener
    }

    override fun onCreateHolder(parent: ViewGroup?, viewType: Int): BannerItemViewHolder {
        val bannerBinding: ItemViewpagerLayoutBinding =
            DataBindingUtil.inflate(
                LayoutInflater.from(parent?.context),
                R.layout.item_viewpager_layout,
                parent,
                false
            )
        return BannerItemViewHolder(bannerBinding)
    }

    override fun onBindView(
        holder: BannerItemViewHolder?,
        data: TopBannerData?,
        position: Int,
        size: Int
    ) {
        var bannerData = mDatas[position]
        bannerData.currentPage = position + 1
        bannerData.pageSize = mDatas.size
        holder?.bind(bannerData)
    }

    inner class BannerItemViewHolder(var binding: ItemViewpagerLayoutBinding) :
        RecyclerView.ViewHolder(binding.root) {
        fun bind(data: TopBannerData) {
            binding.model = data
            binding.bannerLayout.setOnClickListener {
                mOnBannerClickListener.invoke(it,data.url,data.title)
            }
            binding.executePendingBindings()
        }
    }
}